{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Demographic Prisoner's Dilemma\n",
    "\n",
    "The Demographic Prisoner's Dilemma is a family of variants on the classic two-player [Prisoner's Dilemma](https://en.wikipedia.org/wiki/Prisoner's_dilemma), first developed by [Joshua Epstein](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.8.8629&rep=rep1&type=pdf). The model consists of agents, each with a strategy of either Cooperate or Defect. Each agent's payoff is based on its strategy and the strategies of its spatial neighbors. After each step of the model, the agents adopt the strategy of their neighbor with the highest total score. \n",
    "\n",
    "The specific variant presented here is adapted from the [Evolutionary Prisoner's Dilemma](http://ccl.northwestern.edu/netlogo/models/PDBasicEvolutionary) model included with NetLogo. Its payoff table is a slight variant of the traditional PD payoff table:\n",
    "\n",
    "<table>\n",
    "    <tr><td></td><td>**Cooperate**</td><td>**Defect**</td></tr>\n",
    "    <tr><td>**Cooperate**</td><td>1, 1</td><td>0, *D*</td></tr>\n",
    "    <tr><td>**Defect**</td><td>*D*, 0</td><td>0, 0</td></tr>\n",
    "</table>\n",
    "\n",
    "Where *D* is the defection bonus, generally set higher than 1. In these runs, the defection bonus is set to $D=1.6$.\n",
    "\n",
    "The Demographic Prisoner's Dilemma demonstrates how simple rules can lead to the emergence of widespread cooperation, despite the Defection strategy dominiating each individual interaction game. However, it is also interesting for another reason: it is known to be sensitive to the activation regime employed in it.\n",
    "\n",
    "Below, we demonstrate this by instantiating the same model (with the same random seed) three times, with three different activation regimes: \n",
    "\n",
    "* Sequential activation, where agents are activated in the order they were added to the model;\n",
    "* Random activation, where they are activated in random order every step;\n",
    "* Simultaneous activation, simulating them all being activated simultaneously.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from model import PdGrid\n",
    "\n",
    "%matplotlib inline"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Helper functions"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "from mesa.visualization.mpl_space_drawing import draw_space\n",
    "\n",
    "\n",
    "def agent_portroyal(agent):\n",
    "    return {\"color\": \"red\" if agent.move == \"D\" else \"blue\", \"marker\": \"s\", \"size\": 11}"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "def run_model(model):\n",
    "    \"\"\"\n",
    "    Run an experiment with a given model, and plot the results.\n",
    "    \"\"\"\n",
    "    fig = plt.figure(figsize=(12, 8))\n",
    "\n",
    "    ax1 = fig.add_subplot(231)\n",
    "    ax2 = fig.add_subplot(232)\n",
    "    ax3 = fig.add_subplot(233)\n",
    "    ax4 = fig.add_subplot(212)\n",
    "\n",
    "    draw_space(model.grid, agent_portroyal, ax=ax1, draw_grid=False)\n",
    "    model.run(10)\n",
    "    draw_space(model.grid, agent_portroyal, ax=ax2, draw_grid=False)\n",
    "    model.run(10)\n",
    "    draw_space(model.grid, agent_portroyal, ax=ax3, draw_grid=False)\n",
    "    model.datacollector.get_model_vars_dataframe().plot(ax=ax4)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "# Set the random seed\n",
    "seed = 21"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sequential Activation"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "pd_model = PdGrid(50, 50, \"Sequential\", seed=seed)\n",
    "run_model(pd_model)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Random Activation"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "pd_model = PdGrid(50, 50, \"Random\", seed=seed)\n",
    "run_model(pd_model)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "source": [
    "## Simultaneous Activation"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "pd_model = PdGrid(50, 50, \"Simultaneous\", seed=seed)\n",
    "run_model(pd_model)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [],
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
